﻿using Microsoft.Practices.EnterpriseLibrary.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TN_CMS.Domain.Abstract;
using TN_CMS.Domain.ResultSetMapper;

namespace TN_CMS.Domain.Concrete
{
    public class CommonRepository : BaseRepository, ICommonRepository
    {
        public int GetMax(string table, string column)
        {
            var map = new IntegerResultSetMapper();
            var query = this._db.CreateSprocAccessor("SP_Common_GetMax", map);

            return query.Execute(table, column).FirstOrDefault();
        }

        public List<int> GetMappingList(string table, string column, string columnId, int id)
        {
            var map = new IntegerResultSetMapper();
            var query = this._db.CreateSprocAccessor("SP_Common_GetMappingList", map);

            return query.Execute(table, column, columnId, id).ToList();
        }

        public int InsertMapping(string table, string column1, string column2, int id1, int id2)
        {
            var map = new IntegerResultSetMapper();
            var query = this._db.CreateSprocAccessor("SP_Common_InsertMapping", map);

            return query.Execute(
                table,
                column1,
                column2,
                id1,
                id2).FirstOrDefault();
        }

        public int UpdateActive(string table, string column, string columnId, string ids, bool isActive)
        {
            var map = new IntegerResultSetMapper();
            var query = this._db.CreateSprocAccessor("SP_Common_UpdateActive", map);

            return query.Execute(
                table,
                column,
                columnId,
                ids,
                isActive
            ).FirstOrDefault();
        }

        public int UpdateToggle(string table, string column, string columnId, int id)
        {
            var map = new IntegerResultSetMapper();
            var query = this._db.CreateSprocAccessor("SP_Common_UpdateToggle", map);

            return query.Execute(table, column, columnId, id).FirstOrDefault();
        }

        public int UpdateIncrease(string table, string column, string columnId, int id, bool isIncrease)
        {
            var map = new IntegerResultSetMapper();
            var query = this._db.CreateSprocAccessor("SP_Common_UpdateIncrease", map);

            return query.Execute(
                table,
                column,
                columnId,
                id,
                isIncrease
                ).FirstOrDefault();
        }

        public int Delete(string table, string columnId, string ids)
        {
            var map = new IntegerResultSetMapper();
            var query = this._db.CreateSprocAccessor("SP_Common_Delete", map);

            return query.Execute(table, columnId, ids).FirstOrDefault();
        }

        public int DeleteMapping(string table, string column1, string column2, int id1, int id2)
        {
            var map = new IntegerResultSetMapper();
            var query = this._db.CreateSprocAccessor("SP_Common_DeleteMapping", map);

            return query.Execute(
                table,
                column1,
                column2,
                id1,
                id2).FirstOrDefault();
        }
    }
}
